<?php
namespace App\Api\User;

use PhalApi\Api;
use App\Domain\User\User as UserDomain;
use App\Domain\User\UserSession as UserSessionDomain;
use PhalApi\Exception\BadRequestException;

/**
 * 获取上级代理信息
 * @author dogstar 20200331
 */
class GetAgent extends Api {
    public function getRules() {
        return array(
            'getByUsername' => array(
                'username' => array('name' => 'username', 'require' => true, 'min' => 1, 'max' => 50, 'desc' => '账号，账号需要唯一'),
            ),
        );
    }

    /**
     * 获取个人信息
     * @desc 获取个人信息
     * 
     * @return int res 是否更新成功
     */
    public function getByUsername() {
        // 最简单的处理方式
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *,x-token,X-token');

        // $this->username = '15818262713' // 获取请求参数
       
        $domain = new UserDomain();
        $user = $domain->getUserByUsername($this->username, '*');
        // $user = $domain->getUserByUsername('15818262713', '*');
        $Invitations = $user['Invitations'];
        $Invitations_usernames = explode(',', $Invitations);

        return false;

        $agent_info = array();
        foreach ($Invitations_usernames as $key => $value) {
            $userInfo = $domain->getUserByUsername($value, 'roleIds, username, nickname');
            $roleIds = json_decode($userInfo['roleIds'], true); // 解析roleIds为数组
            if (is_array($roleIds) && in_array('4', $roleIds)) {
                return $userInfo; // 数组累加避免覆盖
            }
        }
        
    }

    
}
